home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / ZED3DSRC.ZIP / TEST.C < prev    next >
C/C++ Source or Header  |  1995-06-19  |  11KB  |  699 lines

  1. #include <stdlib.h>
  2. #include <conio.h>
  3. #include <stdio.h>
  4. #include <dos.h>
  5. #include <ctype.h>
  6. #include <time.h>
  7. #include <dosfunc.h>
  8. #include <zsort.h>
  9. #include <3ds.h>
  10.  
  11.  
  12. float RAND(void);
  13. object *init(void);
  14. void shutdown(object *o);
  15. void displayobject(object *o);
  16. outbuffer *init256(void);
  17. void free256(outbuffer *b);
  18. void test_d(object *o);
  19. int seeded=0;
  20. int x=0;
  21. float dx=0,dy=0,dz=0;
  22. int drawing=1;
  23. int do_rotation(matrix orientation);
  24.  
  25.  
  26.  
  27. float RAND(void)
  28.     {
  29.     if(!seeded)
  30.         {
  31.         srand(time(0));
  32.         seeded++;
  33.         }
  34.  
  35.     return (rand()/((float)RAND_MAX));
  36.     }
  37.  
  38.  
  39. /***********************************************************************
  40.  Disabled code
  41.  -------------
  42.  ***********************************************************************/
  43. #if 0
  44. unsigned long far *screen;
  45. object *init(void)
  46.     {
  47.     object *o;
  48.     long a,b,c,x,y,z;
  49.     union REGS r;
  50.  
  51.  
  52.     o=alloc_object(100);
  53.  
  54.     if(!o)
  55.         return 0;
  56.  
  57.     for(a=0;a<100;a++)
  58.         {
  59.         for(b=0;b<3;b++)
  60.             {
  61.             o->points->vertex[a].location[b]=
  62.                 floattoreal(RAND()-.5);
  63.             }
  64.         }
  65.  
  66.     r.x.ax=0x13;
  67.     int86(0x10,&r,&r);
  68.  
  69.     outp(0x3c2,0xe3);
  70.  
  71.     screen=_x386_mk_protected_ptr(0xa0000);
  72. /*      screen=((unsigned long far *)0xa0000000);*/
  73.  
  74.     return o;
  75.     }
  76.  
  77.  
  78.  
  79. void shutdown(object *o)
  80.     {
  81.     union REGS r;
  82.  
  83.     r.x.ax=0x3;
  84.  
  85.     int86(0x10,&r,&r);
  86.  
  87.     free_object(o);
  88.     }
  89.  
  90.  
  91.  
  92.  
  93. void displayobject(object *o)
  94.     {
  95.     static unsigned char *vscr=0;
  96.     unsigned long *_v_;
  97.     long a,b,c,xx,yy;
  98.     float x,y,z;
  99.  
  100.     if(!vscr)
  101.         {
  102.         vscr=malloc(64000);
  103.         if(!vscr)
  104.             {
  105.             puts("malloc error in displayobject!");
  106.             abort();
  107.             }
  108.         }
  109.  
  110.  
  111.     _v_=((unsigned long *)vscr);
  112.     for(a=0;a<16000;a++)
  113.         _v_[a]=0;
  114.  
  115.  
  116.     for(a=0;a<o->numpoints;a++)
  117.         {
  118.         x=realtofloat(
  119.             div(
  120.                 o->x_vertex[a].location[0],
  121.                 o->x_vertex[a].location[2]
  122.                 )
  123.             );
  124.         y=realtofloat(
  125.             div(
  126.                 o->x_vertex[a].location[1],
  127.                 o->x_vertex[a].location[2])
  128.             );
  129.  
  130.  
  131.         xx=x*300+160;
  132.         yy=y*300+100;
  133.  
  134.         vscr[xx+yy*320]=15;
  135.         }
  136.  
  137.     for(a=0;a<16000;a++)
  138.         screen[a]=_v_[a];
  139.     }
  140.  
  141.  
  142. void test_a(void)
  143.     {
  144.     object *o;
  145.     matrix orientation;
  146.     vector position;
  147.     int x;
  148.     float dx,dy,dz;
  149.  
  150.  
  151.     initmatrix(orientation,
  152.         1,0,0,
  153.         0,1,0,
  154.         0,0,1);
  155.  
  156.  
  157.     o=init();
  158.     initvector(position,0,0,3);
  159.  
  160.     if(!o)
  161.         {
  162.         puts("Could not initialize stuff");
  163.         return;
  164.         }
  165.  
  166.     x=0;
  167.     while(x!='#')
  168.         {
  169.         xform_object(*o,orientation,position);
  170.         displayobject(o);
  171.         rotatebase(orientation,0,floattoreal(dx));
  172.         rotatebase(orientation,1,floattoreal(dy));
  173.         rotatebase(orientation,2,floattoreal(dz));
  174.         if(kbhit())
  175.             {
  176.             x=getch();
  177.             switch(toupper(x))
  178.                 {
  179.                 case 'Q':
  180.                     dx+=.01;
  181.                     break;
  182.                 case 'A':
  183.                     dx-=.01;
  184.                     break;
  185.                 case 'W':
  186.                     dy+=.01;
  187.                     break;
  188.                 case 'S':
  189.                     dy-=.01;
  190.                     break;
  191.                 case 'E':
  192.                     dz+=.01;
  193.                     break;
  194.                 case 'D':
  195.                     dz-=.01;
  196.                     break;
  197.                 case ' ':
  198.                     dx=0;
  199.                     dy=0;
  200.                     dz=0;
  201.                     break;
  202.                 }
  203.             }
  204.         else
  205.             {
  206.             x=0;
  207.             }
  208. /*              mat_orthonormalize(orientation);*/
  209.         }
  210.  
  211.     shutdown(o);
  212.     }
  213. #endif
  214. /***********************************************************************
  215.  End disabled code
  216.  -----------------
  217.  ***********************************************************************/
  218.  
  219.  
  220. void test_b(void)
  221.     {
  222.     matrix m,r,q;
  223.     int x,y,z;
  224.  
  225.  
  226.     srand(time(0));
  227.     for(x=0;x<3;x++)
  228.         for(y=0;y<3;y++)
  229.             m[x][y]=floattoreal((rand()&3)*1.0);
  230.  
  231.  
  232.     printmatrix(m);
  233.     z=invert(m,r);
  234.  
  235.     if(z)
  236.         {
  237.         printf("Matrix is not inversible, determinant is %f\n",
  238.             realtofloat(determinant(m)));
  239.         }
  240.     else
  241.         {
  242.         puts("Matrix is inversible");
  243.         printmatrix(r);
  244.  
  245.         puts("Matrix product should be identity matrix");
  246.         mat_mul(q,m,r);
  247.         printmatrix(q);
  248.         }
  249.     }
  250.  
  251.  
  252.  
  253.  
  254. object *init_c(void)
  255.     {
  256.     object *o;
  257.     point *p;
  258.     long a,b,c;
  259.  
  260.     o=0;
  261. /*    do
  262.         {*/
  263.         if(o)
  264.             free_object(o);
  265.  
  266.         o=alloc_object(4,4);
  267.         p=o->pts_data->vertex;
  268.         for(a=0;a<4;a++,p++)
  269.             {
  270.             initvector(p->location,(RAND()-.5)*2,(RAND()-.5)*2,(RAND()-.5)*2);
  271.             }
  272.     /*    }
  273.     while(*/make_tetrahedron(o)/*)*/;
  274.  
  275.     normalize_object(o);
  276.  
  277.     return o;
  278.     }
  279.  
  280.  
  281.  
  282. outbuffer *init256(void)
  283.     {
  284.     to256();
  285.     setgrayscale(20,80);
  286.     return allocbuf(99,99);
  287.     }
  288.  
  289.  
  290. void free256(outbuffer *b)
  291.     {
  292.     totxt();
  293.     destroybuf(b);
  294.     }
  295.  
  296.  
  297.  
  298.  
  299. void test_d(object *o)
  300.     {
  301.     pipeline *pip;
  302.     affine camera;
  303.     outbuffer *buf;
  304.     polygon pol;
  305.     edge edg[3];
  306.     face *f;
  307.     point *p;
  308.     long a,b,c;
  309.     long *pp;
  310.     REAL r;
  311.  
  312.     pip=alloc_pipeline(100,100,100);
  313.     if(!pip)
  314.         {
  315.         puts("Memory allocation error");
  316.         return;
  317.         }
  318.  
  319.     initmatrix(camera.m,
  320.         1,0,0,
  321.         0,1,0,
  322.         0,0,1);
  323.     initvector(camera.v,0,0,3);
  324.  
  325.     buf=init256();
  326.     if(!buf)
  327.         {
  328.         totxt();
  329.         puts("Memory allocation error");
  330.         return;
  331.         }
  332.  
  333.  
  334.     do
  335.         {
  336.         reset_pipeline(pip);
  337.         cull_object(o,pip,&camera);
  338.         f=pip->fbase;
  339.  
  340.         memset(buf->buffer,1,buf->width*buf->height);
  341.  
  342.         for(a=0;a<pip->fptr;a++,f++)
  343.             {
  344.             pol.edgetable=edg;
  345.             pol.color=30*f->normal[2]+70;
  346.             for(b=0;b<3;b++)
  347.                 {
  348.                 p=&pip->pbase[f->index[b]].point;
  349.                 edg[b].x1=realtofloat(div(p->location[0],p->location[2]))*500.0;
  350.                 edg[b].y1=realtofloat(div(p->location[1],p->location[2]))*500.0;
  351.                 }
  352.             pol.numedges=3;
  353.             drawpolygon(&pol,buf);
  354.             }
  355.  
  356.         blit(buf);
  357.         }
  358.     while(!do_rotation(camera.m));
  359.     free256(buf);
  360.     }
  361.  
  362.  
  363.  
  364. void test_c(void)
  365.     {
  366.     long a,b,c;
  367.     face *f;
  368.     point *p;
  369.     object *o;
  370.     REAL r;
  371.  
  372.     o=init_c();
  373.  
  374.     if(!o)
  375.         {
  376.         puts("Error initializing object");
  377.         return;
  378.         }
  379.  
  380.     printf("Verification of plane equations:\n\n");
  381.     f=o->face_data->face;
  382.     p=o->pts_data->vertex;
  383.     for(a=0;a<o->face_data->numfaces;a++,f++)
  384.         {
  385.         printf("Plane equation:\n%fx+%fy+%fz=%f\nVertices yield:\n",
  386.             realtofloat(f->normal[0]),
  387.             realtofloat(f->normal[1]),
  388.             realtofloat(f->normal[2]),
  389.             realtofloat(f->D)
  390.             );
  391.  
  392.         printvector(f->normal);
  393.         for(b=0;b<f->numpoints;b++)
  394.             {
  395.             printf("%i)",b);
  396.             printvector(p[f->index[b]].location);
  397.             r=vec_dot(f->normal,p[f->index[b]].location);
  398.             r-=f->D;
  399.             printf("%f\n",
  400.                 realtofloat(r)
  401.                 );
  402.             }
  403.         }
  404.  
  405.     puts("numpoly data");
  406.  
  407.     for(a=0;a<4;a++)
  408.         printf("Vertex %li: %li\n",a,o->pts_data->vertex[a].clipping);
  409.  
  410.     puts("\nDone.");
  411.     test_d(o);
  412.     }
  413.  
  414.  
  415.  
  416. int do_rotation(matrix orientation)
  417.     {
  418.     rotatebase(orientation,0,floattoreal(dx));
  419.     rotatebase(orientation,1,floattoreal(dy));
  420.     rotatebase(orientation,2,floattoreal(dz));
  421.     if(kbhit())
  422.         {
  423.         x=getch();
  424.         switch(toupper(x))
  425.             {
  426.             case 'Q':
  427.                 dx+=.01;
  428.                 break;
  429.             case 'A':
  430.                 dx-=.01;
  431.                 break;
  432.             case 'W':
  433.                 dy+=.01;
  434.                 break;
  435.             case 'S':
  436.                 dy-=.01;
  437.                 break;
  438.             case 'E':
  439.                 dz+=.01;
  440.                 break;
  441.             case 'D':
  442.                 dz-=.01;
  443.                 break;
  444.             case ' ':
  445.                 dx=0;
  446.                 dy=0;
  447.                 dz=0;
  448.                 break;
  449.             case '=':
  450.                 drawing=0;
  451.                 break;
  452.             }
  453.         }
  454.     else
  455.         {
  456.         x=0;
  457.         }
  458.  
  459.     if(x=='#')
  460.         return -1;
  461.  
  462.     return 0;
  463.     }
  464.  
  465.  
  466.  
  467.  
  468.  
  469. void test_e(void)
  470.     {
  471.     pipeline *p;
  472.     long a,b,c;
  473.  
  474.  
  475.     p=alloc_pipeline(1,20,1);
  476.     if(!p)
  477.         {
  478.         puts("Memory allocation error");
  479.         return;
  480.         }
  481.  
  482.     puts("Initial state:");
  483.     for(a=0;a<20;a++)
  484.         {
  485.         p->maxZ[a]=(RAND()-.5)*65536*2;
  486.         printf("%8lX\n",p->maxZ[a]);
  487.         }
  488.  
  489.     puts("Sorting...");
  490.     sort_pipeline(p);
  491.     puts("Done.");
  492.     }
  493.  
  494.  
  495.  
  496.  
  497.  
  498. void test_f(void)
  499.     {
  500.     object *o,*oo;
  501.     pipeline *pip;
  502.     affine camera;
  503.     outbuffer *buf;
  504.     polygon pol;
  505.     edge edg[3];
  506.     face *f;
  507.     point *p;
  508.     long a,b,c;
  509.     long *pp;
  510.     REAL r;
  511.  
  512.  
  513.     o=init_c();
  514.     oo=init_c();
  515.  
  516.     for(a=0;a<4;a++)
  517.         {
  518.         o->pts_data->vertex[a].location[2]+=1.5;
  519.         }
  520.  
  521.     for(a=0;a<4;a++)
  522.         {
  523.         oo->pts_data->vertex[a].location[2]-=1.5;
  524.         }
  525.  
  526.     pip=alloc_pipeline(100,100,100);
  527.     if(!pip)
  528.         {
  529.         puts("Memory allocation error");
  530.         return;
  531.         }
  532.  
  533.     initmatrix(camera.m,
  534.         1,0,0,
  535.         0,1,0,
  536.         0,0,1);
  537.     initvector(camera.v,0,0,4);
  538.  
  539.     buf=init256();
  540.     if(!buf)
  541.         {
  542.         totxt();
  543.         puts("Memory allocation error");
  544.         return;
  545.         }
  546.  
  547.  
  548.     do
  549.         {
  550.         reset_pipeline(pip);
  551.         cull_object(o,pip,&camera);
  552.         cull_object(oo,pip,&camera);
  553.         sort_pipeline(pip);
  554.         memset(buf->buffer,1,buf->width*buf->height);
  555.  
  556.         for(a=pip->fptr-1;a>=0;a--)
  557.             {
  558.             f=pip->fbase+pip->index1[a];
  559. /*                      f=pip->fbase+a;*/
  560.  
  561.             pol.edgetable=edg;
  562.             pol.color=30*f->normal[2]+70;
  563.             for(b=0;b<3;b++)
  564.                 {
  565.                 p=&pip->pbase[f->index[b]].point;
  566.                 edg[b].x1=realtofloat(div(p->location[0],p->location[2]))*500.0;
  567.                 edg[b].y1=realtofloat(div(p->location[1],p->location[2]))*500.0;
  568.                 }
  569.             pol.numedges=3;
  570.             if(drawing)
  571.                 drawpolygon(&pol,buf);
  572.             }
  573.  
  574.         blit(buf);
  575.         }
  576.     while(!do_rotation(camera.m));
  577.     free256(buf);
  578.     }
  579.  
  580.  
  581.  
  582.  
  583.  
  584. void test_g(void)
  585.     {
  586.     object *o;
  587.     pipeline *pip;
  588.     affine camera;
  589.     outbuffer *buf;
  590.     polygon pol;
  591.     edge edg[3];
  592.     face *f;
  593.     pipe_point *p;
  594.     long a,b,c;
  595.     long *pp;
  596.     REAL r;
  597.     float A,B,C;
  598.     long time0,time1,polycount;
  599.     float total_time;
  600.  
  601.  
  602.     o=read_3ds_file("duck.asc",1);
  603.  
  604.     if(!o)
  605.         {
  606.         puts("Error reading file");
  607.         return;
  608.         }
  609.     A=B=C=0;
  610.     for(a=0;a<o->pts_data->numpoints;a++)
  611.         {
  612.         A+=realtofloat(o->pts_data->vertex[a].location[0]);
  613.         B+=realtofloat(o->pts_data->vertex[a].location[1]);
  614.         C+=realtofloat(o->pts_data->vertex[a].location[2]);
  615.         }
  616.     A/=o->pts_data->numpoints;
  617.     B/=o->pts_data->numpoints;
  618.     C/=o->pts_data->numpoints;
  619.     for(a=0;a<o->pts_data->numpoints;a++)
  620.         {
  621.         o->pts_data->vertex[a].location[0]-=floattoreal(A);
  622.         o->pts_data->vertex[a].location[1]-=floattoreal(B);
  623.         o->pts_data->vertex[a].location[2]-=floattoreal(C);
  624.         }
  625.  
  626.     pip=alloc_pipeline(300,700,2100);
  627.     if(!pip)
  628.         {
  629.         puts("Memory allocation error");
  630.         return;
  631.         }
  632.  
  633.     initmatrix(camera.m,
  634.         1,0,0,
  635.         0,1,0,
  636.         0,0,1);
  637.     initvector(camera.v,0,0,3000);
  638.  
  639.     buf=init256();
  640.     if(!buf)
  641.         {
  642.         totxt();
  643.         puts("Memory allocation error");
  644.         return;
  645.         }
  646.  
  647.     polycount=0;
  648.     time0=clock();
  649.  
  650.     do
  651.         {
  652.         reset_pipeline(pip);
  653.         cull_object(o,pip,&camera);
  654.         sort_pipeline(pip);
  655.         memset(buf->buffer,1,buf->width*buf->height);
  656.  
  657.         for(a=pip->fptr-1;a>=0;a--)
  658.             {
  659.             f=pip->fbase+pip->index1[a];
  660. /*                      f=pip->fbase+a;*/
  661.  
  662.             pol.edgetable=edg;
  663.             pol.color=realtofloat(mul(f->normal[2],floattoreal(30)))+70;
  664.             for(b=0;b<3;b++)
  665.                 {
  666.                 p=&pip->pbase[f->index[b]];
  667.                 edg[b].x1=realtofloat(p->scr_X)*500.0;
  668.                 edg[b].y1=realtofloat(p->scr_Y)*500.0;
  669.                 }
  670.             pol.numedges=3;
  671.             if(drawing)
  672.                 drawpolygon(&pol,buf);
  673.             }
  674.  
  675.         blit(buf);
  676.         mat_orthonormalize(camera.m);
  677.         polycount+=o->face_data->numfaces;
  678.         }
  679.     while(!do_rotation(camera.m));
  680.  
  681.     time1=clock();
  682.  
  683.     free256(buf);
  684.  
  685.     total_time=(time1-time0)/CLK_TCK;
  686.  
  687.     printf("Total time elapsed: %f seconds\nTotal # of polygon blitted: %li\nPolygons per second: %f\n\n",
  688.         total_time,polycount,polycount/total_time);
  689.     }
  690.  
  691.  
  692.  
  693.  
  694.  
  695. void main(void)
  696.     {
  697.     test_g();
  698.     }
  699.